<template>
	<view class="form-wrap">
		<view class="label"><text class="request">*</text>批次号</view>
		<view class="input-wrap flex-row">
			<input v-model="form.batchNo" @input="getInStockBatchInfo" class="input" type="text"
				placeholder="请扫描/输入批次号" />
			<image v-if="form.batchNo" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="clearBatchNo"></image>
		</view>

		<view class="label"><text class="request">*</text>库位编码</view>
		<view class="input-wrap flex-row">
			<input v-model="form.storehouseLocationNo" class="input" type="text" placeholder="请扫描/输入库位编码" />
			<image v-if="form.storehouseLocationNo" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="clearLocationNo"></image>
		</view>

		<view class="label">物料编码</view>
		<view class="input-wrap">
			<input v-model="form.productNo" disabled class="input" type="text" placeholder="物料编码" />
		</view>

		<view class="label">物料名称</view>
		<view class="input-wrap">
			<input v-model="form.productName" disabled class="input" type="text" placeholder="物料名称" />
		</view>

		<view class="label"><text class="request">*</text>数量</view>
		<view class="input-wrap flex-row">
			<input v-model="form.quantity" class="input" type="number" @input="setNum" placeholder="请输入数量" />
			<image v-if="form.quantity" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="clearQuantity"></image>
		</view>
		
		<view class="label">订单编号</view>
		<view class="input-wrap">
			<input v-model="form.orderNo" disabled class="input" type="text" placeholder="请扫描/输入订单编号" />
		</view>
		<button @click="submit" class="submit-btn">确定</button>
	</view>
</template>

<script>
	import {
		getInStockBatchInfo,
		inoutstock
	} from "./api"
	var printModule = uni.requireNativePlugin("pda-print-PdaPrintModule")
	export default {
		data() {
			return {
				form: {
					batchNo: '',
					storehouseLocationNo: '',
					productNo: '',
					productName: '',
					quantity: '',
					orderNo:''
				}
			}
		},
		methods: {
			clearForm(){
				this.form = {
					batchNo: '',
					storehouseLocationNo: '',
					productNo: '',
					productName: '',
					quantity: '',
					orderNo:''
				}
			},
			// 验证大于等于1的整数
			setNum(e) {
				if (e.target.value) {
					let newValue = e.target.value.replace(/^\D*(\d*).*$/g, "$1")
					newValue = Math.max(1, newValue)
					this.$nextTick(() => {
						this.form.quantity = newValue
					})
				}
			},
			getInStockBatchInfo(e) {
				if (e.target.value === "") {
					this.$modal.msg("请输入批次号")
					this.clearForm()
				}else{
					getInStockBatchInfo(e.target.value).then(res => {
						if (res.code == 200) {
							this.form = res.data
						}
					})
				}
			},
			submit() {
				if (!this.form.batchNo ) {
					this.$modal.msg("请输入批次号")
				} else if (!this.form.storehouseLocationNo ) {
					this.$modal.msg("请输入库位编码")
				} else if (!this.form.quantity  || this.form.quantity == 0) {
				 this.$modal.msg("请输入数量")
				} else {
					this.$modal.loading("入库中...")
					inoutstock({...this.form, type:0, operation:0}).then(res => {
						this.$modal.closeLoading()
						if (res.code == 200) {
							this.$modal.msgSuccess("入库成功")
							this.clearForm()
						}
					})
				}
			},
		}
	}
</script>

<style>
</style>
